import cv2
import numpy as np

image = cv2.imread('16.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 显示gray
cv2.imshow('gray', gray)
cv2.waitKey(0)
_, thresholded = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)
_, contours, _= cv2.findContours(thresholded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

valid_contours = []

for contour in contours:
    x, y, w, h = cv2.boundingRect(contour)
    if w > 10 and h > 10:
        valid_contours.append(contour)
total_paper_area = image.shape[0] * image.shape[1]
printed_area = 0

for contour in valid_contours:
    printed_area += cv2.contourArea(contour)

print('印刷完整度：', printed_area / total_paper_area * 100, '%')


